<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button
      type="primary"
      size="small"
      :disabled="!message.length"
      @click="setCommont"
    >发布</van-button>
  </div>
</template>

<script>
import { setCommonts } from '@/api/commont'
export default {
  name: 'PostComment',
  components: {},
  props: {
    artId: { // 评论id 或者文章id
      type: [Object, String, Number],
      required: true
    },
    comId: {
      type: [Object, String, Number]
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async setCommont () {
      try {
        const { data } = await setCommonts({
          target: this.artId,
          content: this.message,
          art_id: this.artId === this.comId ? null : this.comId
        })
        this.message = ''
        this.$toast.success('评论成功')
        this.$emit('update', data.data)
      } catch (err) {
        this.$toast.fail('评论失败，请重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  padding: 15px;
  align-items: center;
  .post-field {
    background: #f5f7f9;
    margin-right: 15px;
  }
}
</style>
